"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defines_1 = require("../lib/defines");
function dissolveClassroom(app) {
    const { socket, rooms } = app.locals;
    const { userId, username } = socket.handshake.query;
    const { dissolve } = defines_1.RequestMethod;
    socket.on(dissolve, function dissolve(room, cb) {
        const __room = rooms[room.classId];
        const member = { userId, username };
        if (__room) {
            room = Object.assign(room, __room.json());
            socket
                .timeout(1000 * 2)
                .to(room.classId)
                .emit('dissolve', { room, member }, () => {
                __room.getMembers().forEach(member => {
                    member.stop(userId);
                });
                rooms[room.classId] = null;
                if (cb) {
                    cb({ status: 'ok', resolve: 'dissolved' });
                }
            });
        }
        else {
            rooms[room.classId] = null;
            console.error('room not find!');
        }
    });
}
exports.default = dissolveClassroom;
